package org.example.autofilearchiver.Web.ServiceImpl;


import org.example.autofilearchiver.tool.DOCX.DocxTool;
import org.example.autofilearchiver.tool.PDF.PDFTool;
import org.example.autofilearchiver.tool.StaticClass.DOCX.DocxStatic;
import org.example.autofilearchiver.tool.StaticClass.FILE.FileStatic;
import org.example.autofilearchiver.tool.StaticClass.PDf.PDFStatic;
import org.example.autofilearchiver.tool.String.StringTool;
import org.example.autofilearchiver.tool.fileTool.FileUtils;
import org.example.autofilearchiver.toolclass.DOCX.DocxFile;
import org.example.autofilearchiver.toolclass.DOCX.DocxFileTemplate;
import org.example.autofilearchiver.toolclass.ELSE.FindDataWithStringAndNumberObject;
import org.example.autofilearchiver.toolclass.PDF.PDFFile;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PdfProcessServiceImpl {
    /**
     * 处理没有匹配到模板的pdf对象，直接将其和前面页合并
     */
    public static void processPdfObjectWithoutTemplate() throws IOException {
        List<PDFFile> pdfObjects = PDFStatic.pdfObjects;
        for (PDFFile pdfObject : pdfObjects) {
            List<PDFFile> pdfFiles = pdfObject.getPdfFiles();
            //存pdf的绝对路径
            List<String> pdfPaths = new ArrayList<>();
            //存模板对象
            List<DocxFileTemplate> docxFileTemplates = new ArrayList<>();
            for (PDFFile pdfFile : pdfFiles) {
                pdfPaths.add(pdfFile.getPdfPath());
                DocxFileTemplate docxFileTemplate = pdfFile.getDocxFileTemplate();
                docxFileTemplates.add(docxFileTemplate);
            }
            //根据有无模板对象进行合并
            List<List<DocxFileTemplate>> lists = DocxTool.groupDocxFileTemplates(docxFileTemplates);
            int i = 0;
            for (List<DocxFileTemplate> list : lists) {
                ArrayList<String> pdfPathsTemp = new ArrayList<>();
                for (DocxFileTemplate docxFileTemplate : list) {
                    pdfPathsTemp.add(pdfPaths.get(i));
                    i++;
                }
                //合并pdf
                PDFTool.mergePDFs(pdfPathsTemp);
            }
            //移除被合并的pdf对象
            Iterator<PDFFile> iterator = pdfFiles.iterator();
            while (iterator.hasNext()) {
                PDFFile pdfFile = iterator.next();
                if (pdfFile.getDocxFileTemplate() == null) {
                    iterator.remove(); // 使用 Iterator 的 remove 方法
                }
            }
        }
    }

    /**
     * 功能1用的共有字匹配
     */
    public static void findCommonSubstringCanUse() {
        List<DocxFile> docxObjects = DocxStatic.docxObjects;
        for (int i = 0; i < docxObjects.size(); i++) {
            //每个docx，进行一个共有字段提取就行了
            DocxFile docxObject = docxObjects.get(i);
            List<PDFFile> pdfFiles = docxObject.getPdfFiles();
            List<String> needGetCommonString = new ArrayList<>();
            pdfFiles.forEach(pdfFile -> {
                //每个pdf获取到有用的usefulstr，有共性提取就提取
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = pdfFile.getDocxFileTemplate().getFindDataWithStringAndNumberObjects();
                findDataWithStringAndNumberObjects.forEach(findDataWithStringAndNumberObject -> {
                    boolean extractCommonFeature = findDataWithStringAndNumberObject.getIsExtractCommonFeature();
                    if (extractCommonFeature) {
                        needGetCommonString.add(findDataWithStringAndNumberObject.getUsefulString());
                    }
                });
            });
            //把共有字赋值一遍
            String commonPrefix = StringTool.findCommonPrefix(needGetCommonString);
            pdfFiles.forEach(pdfFile -> {
                //每个pdf获取到有用的usefulstr，赋值
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = pdfFile.getDocxFileTemplate().getFindDataWithStringAndNumberObjects();
                findDataWithStringAndNumberObjects.forEach(findDataWithStringAndNumberObject -> {
                    boolean extractCommonFeature = findDataWithStringAndNumberObject.getIsExtractCommonFeature();
                    if (extractCommonFeature) {
                        findDataWithStringAndNumberObject.setUsefulString(commonPrefix);
                    }
                });
            });


        }
    }

    /**
     * 功能2用的共有字提取
     */
    public static void findCommonSubstringCanUse2() {
        //遍历每个pdf
        List<PDFFile> pdfObjects = PDFStatic.pdfObjects;
        for (int i = 0; i < pdfObjects.size(); i++) {
            PDFFile pdfObject = pdfObjects.get(i);
            //遍历pdf对象的单页对象
            List<PDFFile> pdfFiles = pdfObject.getPdfFiles();
            //需要共有字提取放这
            List<String> needGetCommonString = new ArrayList<>();
            for (int i1 = 0; i1 < pdfFiles.size(); i1++) {
                PDFFile pdfFile = pdfFiles.get(i1);
                DocxFileTemplate docxFileTemplate = pdfFile.getDocxFileTemplate();
                if (docxFileTemplate == null) {
                    continue;
                }
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = pdfFile.getDocxFileTemplate().getFindDataWithStringAndNumberObjects();
                findDataWithStringAndNumberObjects.forEach(findDataWithStringAndNumberObject -> {
                    boolean extractCommonFeature = findDataWithStringAndNumberObject.getIsExtractCommonFeature();
                    if (extractCommonFeature) {
                        needGetCommonString.add(findDataWithStringAndNumberObject.getUsefulString());
                    }
                });
            }
            //进行共有字提取，并重新赋值
            String commonPrefix = StringTool.findCommonPrefix(needGetCommonString);
            for (int i1 = 0; i1 < pdfFiles.size(); i1++) {
                PDFFile pdfFile = pdfFiles.get(i1);
                DocxFileTemplate docxFileTemplate = pdfFile.getDocxFileTemplate();
                if (docxFileTemplate == null) {
                    continue;
                }
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = pdfFile.getDocxFileTemplate().getFindDataWithStringAndNumberObjects();
                findDataWithStringAndNumberObjects.forEach(findDataWithStringAndNumberObject -> {
                    boolean extractCommonFeature = findDataWithStringAndNumberObject.getIsExtractCommonFeature();
                    if (extractCommonFeature) {
                        if (commonPrefix.equals("")) {
                            findDataWithStringAndNumberObject.setUsefulString(Long.toString(System.currentTimeMillis()));
                        } else {
                            findDataWithStringAndNumberObject.setUsefulString(commonPrefix);
                        }

                    }
                });
            }
        }


    }

    /**
     * 提取共有字，这玩意确实能用，但是和需求完全对不上，是一坨复杂的屎，留着，没准以后会用
     */
    public static void findCommonSubstring() {
        //遍历docx对象，遍历每个单页pdf，将其按模板进行分类，对每个分类下的pdf进行共有字提取
        //得到模板的所有名字
        List<DocxFileTemplate> docxFileTemplates = DocxStatic.docxFileTemplates;
        List<String> docxFileTemplateIds = new ArrayList<>();
        docxFileTemplates.forEach(docxFileTemplate -> {
            docxFileTemplateIds.add(docxFileTemplate.getId());
        });
        //按模板的id遍历符合条件的pdf得到共有字
        List<DocxFile> docxObjects = DocxStatic.docxObjects;
        docxObjects.forEach(docxObject -> {
            List<PDFFile> pdfFiles = docxObject.getPdfFiles();
            //按模板的id遍历符合条件的pdf得到共有字
            //先对pdf进行分类按照模板id
            List<List<PDFFile>> pdfsById = new ArrayList<>();
            //每个id建一个集合
            for (int i = 0; i < docxFileTemplateIds.size(); i++) {
                List<PDFFile> pdfs = new ArrayList<>();
                pdfsById.add(pdfs);
            }
            //根据id，把pdf对象放进去,此处实现pdf对象按模板类别进行分类
            for (int i = 0; i < pdfFiles.size(); i++) {
                PDFFile pdfFile = pdfFiles.get(i);
                String pdfId = pdfFile.getDocxFileTemplate().getId();
                int index = docxFileTemplateIds.indexOf(pdfId);
                pdfsById.get(index).add(pdfFile);
            }
            //遍历pdfsById，提取共有字
            for (int i = 0; i < pdfsById.size(); i++) {
                List<PDFFile> pdfFiles1 = pdfsById.get(i);
                //此处有可能有多个FindDataWithStringAndNumberObject，需要进行共性提取，需要再进行一次分类
                //得到所有的findDataWithStringAndNumberObject的种类
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjectCategory = new ArrayList<>();
                PDFFile pdfFile1 = pdfFiles1.get(0);
                //根据findDataWithStringAndNumberObject种类分别得到对应的findDataWithStringAndNumberObjects
                List<List<FindDataWithStringAndNumberObject>> FindDataWithStringAndNumberObjectCategoryByTags = new ArrayList<>();
                for (int i1 = 0; i1 < pdfFile1.getDocxFileTemplate().getFindDataWithStringAndNumberObjects().size(); i1++) {
                    findDataWithStringAndNumberObjectCategory.add(pdfFile1.getDocxFileTemplate().getFindDataWithStringAndNumberObjects().get(i1));
                    FindDataWithStringAndNumberObjectCategoryByTags.add(new ArrayList<>());
                }
                for (int i1 = 0; i1 < pdfFiles1.size(); i1++) {
                    PDFFile pdfFile = pdfFiles1.get(i1);
                    List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = pdfFile.getDocxFileTemplate().getFindDataWithStringAndNumberObjects();
                    for (int i2 = 0; i2 < findDataWithStringAndNumberObjects.size(); i2++) {
                        FindDataWithStringAndNumberObject findDataWithStringAndNumberObject = findDataWithStringAndNumberObjects.get(i2);
                        for (int i3 = 0; i3 < findDataWithStringAndNumberObjectCategory.size(); i3++) {
                            if (findDataWithStringAndNumberObjectCategory.get(i3).getTag().equals(findDataWithStringAndNumberObject.getTag())) {
                                FindDataWithStringAndNumberObjectCategoryByTags.get(i3).add(findDataWithStringAndNumberObject);
                            }
                        }
                    }
                }
                //提取共有字
                List<List<String>> commonStringByTag = new ArrayList<>();
                for (int i1 = 0; i1 < FindDataWithStringAndNumberObjectCategoryByTags.size(); i1++) {
                    List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = FindDataWithStringAndNumberObjectCategoryByTags.get(i1);
                    List<String> str = new ArrayList<>();
                    for (int i2 = 0; i2 < findDataWithStringAndNumberObjects.size(); i2++) {
                        str.add(findDataWithStringAndNumberObjects.get(i2).getUsefulString());
                    }
                    commonStringByTag.add(str);
                }
                for (int i1 = 0; i1 < commonStringByTag.size(); i1++) {
                    String commonPrefix = StringTool.findCommonPrefix(commonStringByTag.get(i1));
                    List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = FindDataWithStringAndNumberObjectCategoryByTags.get(i1);
                    for (int i2 = 0; i2 < findDataWithStringAndNumberObjects.size(); i2++) {
                        findDataWithStringAndNumberObjects.get(i2).setUsefulString(commonPrefix);
                    }
                }

            }
        });
    }

    /**
     * 得到每个pdf的重命名名字和归档路径,功能1用的
     */
    public static void getRenamedPdfNamesAndArchivePaths() {
        //遍历单页pdf对象
        //根据usage进行不同处理，初定文件名用于重命名文件，文件路径用于改文件夹路径
        List<DocxFile> docxObjects = DocxStatic.docxObjects;
        for (int i = 0; i < docxObjects.size(); i++) {
            DocxFile docxFile = docxObjects.get(i);
            List<PDFFile> pdfFiles = docxFile.getPdfFiles();
            for (int i1 = 0; i1 < pdfFiles.size(); i1++) {
                //得到每页对应的文件模板对象
                PDFFile pdfFile = pdfFiles.get(i1);
                DocxFileTemplate docxFileTemplate = pdfFile.getDocxFileTemplate();
                //遍历List<FindDataWithStringAndNumberObject>，根据FindDataWithStringAndNumberObject的usage属性进行不同处理
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = docxFileTemplate.getFindDataWithStringAndNumberObjects();
                String fileName = docxFileTemplate.getName();
                String archiverPath = docxFileTemplate.getArchivePath();
                for (int i2 = 0; i2 < findDataWithStringAndNumberObjects.size(); i2++) {
                    FindDataWithStringAndNumberObject findDataWithStringAndNumberObject = findDataWithStringAndNumberObjects.get(i2);
                    String usage = findDataWithStringAndNumberObject.getUsage();
                    if (usage.equals("文件名")) {
                        //1文件名。获取到所有usage为文件名usefulString，将所有的usefulString连接，作为文件名
                        fileName = (fileName + "-" + findDataWithStringAndNumberObject.getUsefulString());
                    } else if (usage.equals("文件路径")) {
                        //2文件路径，根据归档路径和替换词，生成对应的文件路径
                        archiverPath = archiverPath.replace(findDataWithStringAndNumberObject.getReplacePathString(), findDataWithStringAndNumberObject.getUsefulString());
                    }

                }
                archiverPath = (archiverPath + "\\" + fileName + "--1.pdf");
                pdfFile.setArchiverPath(archiverPath);
            }
        }

    }

    /**
     * 得到每个pdf的重命名名字和归档路径,功能2用的
     */
    public static void getRenamedPdfNamesAndArchivePaths2() {
        //遍历单页pdf对象得到用途
        List<PDFFile> pdfObjects = PDFStatic.pdfObjects;
        for (int i = 0; i < pdfObjects.size(); i++) {
            PDFFile pdfFile = pdfObjects.get(i);
            List<PDFFile> pdfFiles = pdfFile.getPdfFiles();
            for (int j1 = 0; j1 < pdfFiles.size(); j1++) {
                PDFFile pdfFileSingle = pdfFiles.get(j1);
                DocxFileTemplate docxFileTemplate = pdfFileSingle.getDocxFileTemplate();
                if (docxFileTemplate == null) {
                    continue;
                }
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = pdfFileSingle.getDocxFileTemplate().getFindDataWithStringAndNumberObjects();
                String fileName = pdfFileSingle.getDocxFileTemplate().getName();
                String archiverPath = pdfFileSingle.getDocxFileTemplate().getArchivePath();
                for (int i1 = 0; i1 < findDataWithStringAndNumberObjects.size(); i1++) {
                    FindDataWithStringAndNumberObject findDataWithStringAndNumberObject = findDataWithStringAndNumberObjects.get(i1);
                    String usage = findDataWithStringAndNumberObject.getUsage();
                    if (usage.equals("文件名")) {
                        //1文件名。获取到所有usage为文件名usefulString，将所有的usefulString连接，作为文件名
                        fileName = (fileName + "-" + findDataWithStringAndNumberObject.getUsefulString());
                    } else if (usage.equals("文件路径")) {
                        //2文件路径，根据归档路径和替换词，生成对应的文件路径
                        archiverPath = archiverPath.replace(findDataWithStringAndNumberObject.getReplacePathString(), findDataWithStringAndNumberObject.getUsefulString());
                    }
                }
                archiverPath = (archiverPath + "\\" + fileName + "--1.pdf");
                pdfFileSingle.setArchiverPath(archiverPath);
            }
        }


    }

    /**
     * pdf路径存到全局，生成pdf对象
     */
    public static void initPdfFiles() {
        //把pdf的路径存到全局
        String pdfPath = PDFStatic.pdfPath;
        //判断路径是文件还是文件还是文件夹,文件夹的话需要读取文件夹的所有文件,文件直接存,存到全局
        boolean directory = FileUtils.isDirectory(pdfPath);
        PDFStatic.pdfFilePaths = new ArrayList<String>();
        if (directory) {
            List<String> allFilePaths = FileUtils.getAllFilePaths(pdfPath);
            PDFStatic.pdfFilePaths.addAll(allFilePaths);
        } else {
            PDFStatic.pdfFilePaths.add(pdfPath);
        }
        //已经获取到所有的pdf路径
        //创建所有的pdf对象,存到全局
        PDFStatic.pdfObjects = new ArrayList<>();
        for (String filePath : PDFStatic.pdfFilePaths) {
            PDFFile pdfFile = new PDFFile();
            pdfFile.setPdfPath(filePath);
            PDFStatic.pdfObjects.add(pdfFile);
        }
    }

    /**
     * 将pdf分成单页，创建相应的单页文件，创建对象，存到docx,这是功能1用的
     */
    public static void pdfSplit() throws IOException {
        //写成123的文件夹存起来
        int newPath = 1;
        int lastIndex = DocxStatic.docxPath.lastIndexOf("\\");
        List<DocxFile> docxObjects = DocxStatic.docxObjects;
        for (DocxFile docxFile : docxObjects) {
            String newStr = DocxStatic.docxPath.substring(0, lastIndex + 1) + "暂时pdf\\" + String.valueOf(newPath) + "\\";
            newPath++;
            PDFFile pdfFile = docxFile.getPdfFile();
            PDFTool.splitPdf(pdfFile.getPdfPath(), newStr);
            //得到生成的pdf文件的路径集合
            List<String> pdfFilePaths = FileUtils.getNextLevelFilesAbsolutePath(newStr);
            ArrayList<PDFFile> pdfFiles = new ArrayList<>();
            docxFile.setPdfFiles(pdfFiles);
            pdfFilePaths.forEach(pdfFilePath -> {
                //归档路径暂时置为空,模板也置为空,有用的tags数据也置为空
                String archiverPath = "";
                PDFFile pdfFile1 = new PDFFile(pdfFilePath, archiverPath, docxFile, null, null, null, null);
                pdfFiles.add(pdfFile1);
            });
        }
    }

    /**
     * 将pdf分成单页，创建相应的单页文件，创建对象，暂存到pdf文件夹下，这是功能2用的
     */
    public static void pdfSplit2() throws IOException {
        //写成123的文件夹存起来
        int newPath = 1;
        int lastIndex = PDFStatic.pdfPath.lastIndexOf("\\");
        List<PDFFile> pdfObjects = PDFStatic.pdfObjects;
        for (PDFFile pdfFile : pdfObjects) {
            String newStr = PDFStatic.pdfPath.substring(0, lastIndex + 1) + "暂时pdf\\" + String.valueOf(newPath) + "\\";
            newPath++;
            PDFTool.splitPdf(pdfFile.getPdfPath(), newStr);
            //得到生成的pdf文件的路径集合
            List<String> pdfFilePaths = FileUtils.getNextLevelFilesAbsolutePath(newStr);
            List<PDFFile> pdfFiles = new ArrayList<>();
            pdfFile.setPdfFiles(pdfFiles);
            pdfFilePaths.forEach(pdfFilePath -> {
                //归档路径暂时置为空,模板也置为空,有用的tags数据也置为空,添加单页对象的文本
                String archiverPath = "";
                PDFFile pdfFile1 = new PDFFile(pdfFilePath, archiverPath, null, null, null, null, null);
                List<String> pdfText = PDFTool.getPdfText(pdfFile1.getPdfPath());
                pdfText = StringTool.splitStringsByWhitespaceAndRemove(pdfText);
                pdfFile1.setAlltext(pdfText);
                pdfFiles.add(pdfFile1);
            });
        }

    }

    /**
     * 根据模板集合再处理pdf单页对象，得到归档路径，模板，有用的tags，这是功能1用的
     *
     * @throws IOException 异常
     */
    public static void getTemplateData() throws IOException {
        List<DocxFile> docxObjects = DocxStatic.docxObjects;
        for (DocxFile docxFile : docxObjects) {
            //循环所有的docx文件对象
            //得到当前docx对象的text，对应的每页pdf对象，模板集合
            List<String> allText = docxFile.getAllText();
            List<PDFFile> pdfFiles = docxFile.getPdfFiles();
            List<DocxFileTemplate> docxFileTemplates = docxFile.getDocxFileTemplates();
            //给每页pdf加模板集合，要求为拷贝，不是引用，方便后面处理
            for (int i = 0; i < docxFileTemplates.size(); i++) {
                DocxFileTemplate docxFileTemplate = docxFileTemplates.get(i);
                PDFFile pdfFile = pdfFiles.get(i);
                pdfFile.setDocxFileTemplate(docxFileTemplate.copy());
            }
            //遍历alltext，遍历pdf对象，取得对应的模板对象
            for (int i = 0; i < allText.size(); i++) {
                String text = allText.get(i);
                for (int i1 = 0; i1 < pdfFiles.size(); i1++) {
                    PDFFile pdfFile = pdfFiles.get(i1);
                    DocxFileTemplate docxFileTemplate = pdfFile.getDocxFileTemplate();
                    //将模板对象的List<FindDataWithStringAndNumberObject>的FindDataWithStringAndNumberObject遍历，取得tag和增量
                    List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = docxFileTemplate.getFindDataWithStringAndNumberObjects();
                    for (int i2 = 0; i2 < findDataWithStringAndNumberObjects.size(); i2++) {
                        FindDataWithStringAndNumberObject findDataWithStringAndNumberObject = findDataWithStringAndNumberObjects.get(i2);
                        String tag = findDataWithStringAndNumberObject.getTag();
                        int addIndex = findDataWithStringAndNumberObject.getAddIndex();
                        boolean extractCommonFeature = findDataWithStringAndNumberObject.getIsExtractCommonFeature();
                        //根据tag匹配alltext，根据增量取得usefulString，将usefulString存到对应属性
                        if (text.contains(tag)) {
                            String usefulString = allText.get(i + addIndex);
                            findDataWithStringAndNumberObject.setUsefulString(usefulString);
                        }

                    }

                }

            }


        }

    }

    /**
     * 根据模板集合再处理pdf单页对象，得到归档路径，模板，有用的tags，这是功能2用的
     *
     * @throws IOException 异常
     */
    public static void getTemplateData2() throws IOException {
        List<PDFFile> pdfObjects = PDFStatic.pdfObjects;
        for (PDFFile pdfFile : pdfObjects) {
            //得到所有的模板对象集合
            List<DocxFileTemplate> docxFileTemplates = DocxStatic.docxFileTemplates;
            //循环所有的pdf文件对象,得到当前pdf对象的单页pdf对象
            List<PDFFile> pdfFiles = pdfFile.getPdfFiles();
            for (PDFFile pdfSingle : pdfFiles) {
                //匹配模板对象，给单页pdf对象加模板对象
                //得到alltext，根据alltext匹配
                List<String> alltext = pdfSingle.getAlltext();
                //得到唯一标识数据，根据唯一标识数据匹配
                for (String s : alltext) {
                    docxFileTemplates.forEach(docxFileTemplate -> {
                        List<String> identifiers = docxFileTemplate.getIdentifiers();
                        for (String identifier : identifiers) {
                            if (s.contains(identifier)) {
                                pdfSingle.setDocxFileTemplate(docxFileTemplate.copy());
                            }
                        }
                    });

                }
                //得到有用的string存起来
                //遍历alltext，匹配到tag，根据增量得到有用的string存起来
                for (int i = 0; i < alltext.size(); i++) {
                    String s = alltext.get(i);
                    //得到模板对象
                    DocxFileTemplate docxFileTemplate = pdfSingle.getDocxFileTemplate();
                    if (docxFileTemplate == null) {
                        continue;
                    }
                    List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = docxFileTemplate.getFindDataWithStringAndNumberObjects();
                    for (int i1 = 0; i1 < findDataWithStringAndNumberObjects.size(); i1++) {
                        FindDataWithStringAndNumberObject findDataWithStringAndNumberObject = findDataWithStringAndNumberObjects.get(i1);
                        String tag = findDataWithStringAndNumberObject.getTag();
                        int addIndex = findDataWithStringAndNumberObject.getAddIndex();
                        //匹配到tag，存usefulstring
                        if (s.contains(tag)) {
                            findDataWithStringAndNumberObject.setUsefulString(alltext.get(i + addIndex));
                        }
                    }
                }
            }
        }

    }

    /**
     * 根据有用的数据得到归档的路径索引0：新名字，之后每级目录的名字
     *
     * @throws IOException 异常
     */
    public static void getArchiverPath() throws IOException {
        List<DocxFile> docxObjects = DocxStatic.docxObjects;
        docxObjects.forEach(docxFile -> {
            List<PDFFile> pdfFiles = docxFile.getPdfFiles();
            for (int i = 0; i < pdfFiles.size(); i++) {
                PDFFile pdfFile = pdfFiles.get(i);
                //根据每页pdf对象的find。。。对象，得到归档的路径
                List<FindDataWithStringAndNumberObject> findDataWithStringAndNumberObjects = pdfFile.getFindDataWithStringAndNumberObject();
                findDataWithStringAndNumberObjects.forEach(findDataWithStringAndNumberObject -> {
                    //根据findDataWithStringAndNumberObject的有用数据，得到归档的路径
                    String usefulString = findDataWithStringAndNumberObject.getUsefulString();
                    //进行路径替换，首先把pdf名字加上(根据用途属性)
                    if (findDataWithStringAndNumberObject.getUsage().equals("name")) {
                        String archivePathTemplate = pdfFile.getDocxFileTemplate().getArchivePath();
                        pdfFile.setArchiverPath(archivePathTemplate + "//" + usefulString);
                    } else if (findDataWithStringAndNumberObject.getUsage().equals("path")) {
                        //进行replace操作
                        String archivePathTemplate = pdfFile.getDocxFileTemplate().getArchivePath();
                        pdfFile.setArchiverPath(archivePathTemplate.replace(findDataWithStringAndNumberObject.getReplacePathString(), usefulString));
                    }

                });
            }
        });
    }

    /**
     * 归档pdf,功能1用的
     */
    public static void archiverPdf() {

        DocxStatic.docxObjects.forEach(docxFile -> {
            docxFile.getPdfFiles().forEach(pdfFile -> {
                try {
                    //判断归档地方是否存在文件，存在的话需要加后缀
                    int suffix = 0;
                    String archiverPath = pdfFile.getArchiverPath();
                    String pdfPath = pdfFile.getPdfPath();
                    File file = new File(archiverPath);
                    while (file.exists()) {
                        suffix++;
                        file = new File(archiverPath.replaceAll("\\d+\\.", suffix + "."));
                        if (!file.exists()) {
                            archiverPath = archiverPath.replaceAll("\\d+\\.", suffix + ".");
                        }
                    }
                    pdfFile.setArchiverPath(archiverPath);
                    FileUtils.moveFile(pdfPath, archiverPath);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        });
    }

    /**
     * 归档pdf,功能2用的
     */
    public static void archiverPdf2() {
        PDFStatic.pdfObjects.forEach(pdfFile -> {
            pdfFile.getPdfFiles().forEach(pdfFile2 -> {
                try {
                    //判断归档地方是否存在文件，存在的话需要加后缀
                    int suffix = 0;
                    String archiverPath = pdfFile2.getArchiverPath();
                    String pdfPath = pdfFile2.getPdfPath();
                    File file = new File(archiverPath);
                    while (file.exists()) {
                        suffix++;
                        file = new File(archiverPath.replaceAll("\\d+\\.", suffix + "."));
                        if (!file.exists()) {
                            archiverPath = archiverPath.replaceAll("\\d+\\.", suffix + ".");
                        }
                    }
                    pdfFile2.setArchiverPath(archiverPath);
                    FileUtils.moveFile(pdfPath, archiverPath);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });

        });
    }

}
